package cn.tinlan.shopcoin.app.koi;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.koi.adapter.KoiListAdapter;
import cn.tinlan.shopcoin.app.koi.model.KoiListItemModel;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;
import io.reactivex.Observable;

/**
 * Created by Mortimer on 2018/12/24. 09:45
 * 佛祖🙏，永无bug。
 */
public class KoiListActivity extends BaseActivity {

    @BindView(R.id.koi_list_title)
    TitleBar titleBar;
    @BindView(R.id.koi_list_refresh)
    SmartRefreshLayout listRefresh;
    @BindView(R.id.koi_list_rv)
    RecyclerView listRV;
    @BindView(R.id.koi_list_et)
    EditText listET;
    @BindView(R.id.koi_list_appeal)
    TextView listAppeal;


    private int page = 1;
    private List<KoiListItemModel> koiListItemModels;
    private KoiListAdapter listAdapter;
    private String itemID = "";
    private int listStatus = 1;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_koi_list;
    }

    @Override
    public void initView() {
        super.initView();
        initTitleView();
        initSmartRefresh();
        initRV();
        initBottomView();
    }

    private void initBottomView() {
        listAppeal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void initRV() {

        if (listAdapter == null) {
            LinearLayoutManager llm = new LinearLayoutManager(KoiListActivity.this, LinearLayoutManager.VERTICAL, false);
            listRV.setLayoutManager(llm);
            koiListItemModels = new ArrayList<>();

            listAdapter = new KoiListAdapter(R.layout.view_koi_listitem,R.layout.view_dcc_head,koiListItemModels);
            listAdapter.setOnItemSubViewClickListener(new KoiListAdapter.OnItemSubViewClickListener() {



                @Override
                public void onItemStatusClick(String id) {
                    KoiOK(id);
                }


            });
            listRV.setAdapter(listAdapter);

        }else {
            listAdapter.notifyDataSetChanged();
        }
    }

    private void initSmartRefresh() {
        listRefresh.setRefreshHeader(new ClassicsHeader(this));
        listRefresh.setRefreshFooter(new ClassicsFooter(this));
        listRefresh.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        listRefresh.setEnableFooterFollowWhenLoadFinished(true);
        listRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadListData(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadListData(page);
            }
        });
    }

    private void initTitleView() {
        String title = getIntent().getStringExtra("title");
        if (title.equals("send")) {
            titleBar.setTitle("送出锦鲤");
            listStatus = 1;
        }else {
            titleBar.setTitle("接受锦鲤");
            listStatus = 2;
        }


        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        page = 1;
        loadListData(page);
    }

    private void loadListData(final int page1) {
        String url = "";
        if (listStatus == 1) {
            url = Constants.KoiSupportPayment;
        }else {
            url = Constants.KoiSupportIncome;
        }

        Observable<List<KoiListItemModel>> observable = EasyHttp.get(url)
                .params("user_id", SPUtils.getUserID(KoiListActivity.this))
                .params("page",page1+"")
                .execute(new CallClazzProxy<CustomApiResult<List<KoiListItemModel>>, List<KoiListItemModel>>(new TypeToken<List<KoiListItemModel>>() {}.getType()) {
                });

        observable.subscribe(new BaseSubscriber<List<KoiListItemModel>>() {
            @Override
            public void onError(ApiException e) {

                if (page > 1){
                    page--;
                }else {
                    page = 1;
                }

                ToastUtils.show("数据请求失败");

            }

            @Override
            public void onNext(List<KoiListItemModel> KoiListItemModels) {
                super.onNext(KoiListItemModels);

                if (KoiListItemModels.size() < 1){
                    ToastUtils.show("数据暂无");
                    return;
                }

                if (page1 == 1){
                    koiListItemModels = KoiListItemModels;
                    listAdapter.setNewData(koiListItemModels);
                    listRefresh.finishRefresh();
//                    mShopListAdapter.notifyDataSetChanged();
                }else {
                    listAdapter.addData(KoiListItemModels);
                    if (KoiListItemModels.size() < 16) {
                        listRefresh.finishLoadMoreWithNoMoreData();  //全部加载完成,没有数据了调用此方法
                    }else {
                        listRefresh.finishLoadMore();      //加载完成
                    }

//
                }

            }
        });
    }


    private void KoiOK(String id) {
        String url = "";
    if (listStatus == 1) {
        url = Constants.KoiSupportPaymentOK;
    }else {
        url = Constants.KoiSupportIncomeOK;
    }

        EasyHttp.post(url)
                .params("id", id)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");

                    }

                    @Override
                    public void onSuccess(String s) {


//                        ToastUtils.show("添加成功");
                    }
                }) {
                });
    }
}
